#ifndef _THREADTASK_H_
#define _THREADTASK_H_

#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "list.h"
#include "log.h"

#ifdef __cplusplus
extern "C" {
#endif

#define MAX_THREADS 64
#define MAX_TASKS   65535

typedef void (*ThreadTaskFunc_t)(void* ctx, void* param);

typedef struct {
    ThreadTaskFunc_t function;
    void* ctx;
    void* argument;
    uint64_t delay;
} ThreadTaskDelay_t;

typedef enum {
    shutdownNow = 1,
    shutdownGraceFul = 2
} threadTaskShutDown_t;

// typedef struct{
//     uint32_t id;
//     uint64_t delay;
//     ThreadTaskFunc_t *func;
//     void *ctx;
//     void *parm;
// }DelayTask_t;

typedef enum {
    threadpoolInvalid = -1,
    threadpoolLockFailure = -2,
    threadpoolQueueFull = -3,
    threadpoolShutdown = -4,
    threadpoolThreadFailure = -5
} threadTask_errno_t;

typedef enum {
    threadpool_graceful = 1
} threadTask_destroy_flags_t;

typedef struct threadTask {
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    pthread_t* threads;
    ThreadTaskDelay_t* tasks;
    size_t threadNums;
    size_t taskSize;
    size_t head;
    size_t tail;
    size_t taskNums;
    size_t shutdown;
    size_t started;
    struct timespec timeLastSync;
} ThreadTask_t;

ThreadTask_t* ThreadTaskCreate(int32_t threadSize, int32_t taskSize, int32_t flags);

int32_t ThreadTaskAdd(ThreadTask_t* threadTask, ThreadTaskFunc_t func,
                      void* arg, uint64_t delay);

int32_t ThreadTaskDestory(ThreadTask_t* threadTask, int32_t flags);

#ifdef __cplusplus
}
#endif

#endif